package com.skyworthclub.competition.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.skyworthclub.competition.beans.Match;
import com.skyworthclub.competition.beans.User;
import com.skyworthclub.competition.service.MatchService;
import com.skyworthclub.competition.service.UserService;
import com.skyworthclub.competition.utils.web.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户管理前端控制器
 * </p>
 *
 * @author li
 * @since 2018-10-15
 */
@Controller
@RequestMapping("/api/user")
public class UserController {

    private final UserService userService;
    private final MatchService matchService;

    @Autowired
    public UserController(UserService userService, MatchService matchService) {
        this.userService = userService;
        this.matchService = matchService;
    }

    @GetMapping("/info")
    public ResponseEntity<Map<String, Object>> getUserInfo(HttpServletRequest request) {
        String username = request.getUserPrincipal().getName();
        User userInfo = userService.getUserInfo(username);
        userInfo.setPassword(null);
        Map<String, Object> map = new HashMap<>(4);
        map.put("userInfo", userInfo);
        return ResponseUtil.querySuccessResponse(map);
    }

    @GetMapping("/pubCompetitions/{pageNum}")
    public ResponseEntity<Map<String, Object>> getPubCompetitions(@PathVariable("pageNum") Integer pageNum,
                                                                  HttpServletRequest request) {
        String username = request.getUserPrincipal().getName();
        Page<Match> pubCompetitions = matchService.getPubCompetition(username, pageNum);
        Map<String, Object> map = new HashMap<>(4);
        map.put("pubCompetitions", pubCompetitions);
        return ResponseUtil.querySuccessResponse(map);
    }

    @PostMapping("/addFav")
    public ResponseEntity<Map<String, Object>> addFav(@RequestParam("matchId") int matchId,
                                                      HttpServletRequest request){
        String username = request.getUserPrincipal().getName();
        boolean result = userService.addFav(username,matchId);
        Map<String, Object> map = new HashMap<>(4);
        if(result){
            return ResponseUtil.putSuccessResponse(map);
        }else{
            return ResponseUtil.putFailureResponse(map);
        }
    }

    @GetMapping("/showFav/{pageNum}")
    public ResponseEntity<Map<String, Object>> showFav(@PathVariable("pageNum")int pageNum,
                                                       HttpServletRequest request){
        String username = request.getUserPrincipal().getName();
        List<Integer> matchIds = userService.listFavMatchId(username);
        Page<Match> pageObject = matchService.getPageByMatchIds(matchIds, pageNum);
        Map<String, Object> map = new HashMap<>(4);
        map.put("pageObject", pageObject);
        return ResponseUtil.querySuccessResponse(map);
    }

    @DeleteMapping("/removeFav/{matchId}")
    public ResponseEntity<Map<String, Object>> removeFav(@PathVariable("matchId") int matchId,
                                                         HttpServletRequest request){
        String username = request.getUserPrincipal().getName();
        boolean result = userService.removeFav(username,matchId);
        Map<String,Object> map= new HashMap<>(4);
        if(result){
            return ResponseUtil.deleteSuccessResponse(map);
        }else{
            return ResponseUtil.deleteFailureResponse(map);
        }
    }


}

